package com.wandian.news.mvvm.knowledge;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.tools.GDTADTools;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.adapter.NewsKnowledgeRecycleAdapter;
import com.wandian.news.databinding.NewsActivityKnowledgeBinding;
import com.wandian.news.databinding.NewsActivityToutiaoBinding;

import java.util.ArrayList;
import java.util.List;

import static com.mylib.libcore.config.AppConfig.CSJ_BANNER_ONE_ID;
import static com.mylib.libcore.config.AppConfig.GDT_APP_MB_PIC_TWO;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 知识宝库activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NEWS_KNOWLEDGE)
public class KnowledgeActivity extends BaseMVVMActivity<KnowledgeActivityViewModel> {
    private NewsActivityKnowledgeBinding dataBinding;
    private NewsKnowledgeRecycleAdapter newsKnowledgeRecycleAdapter;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_knowledge);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        initRecycle();
        mViewModel.getData();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("知识宝库");

        mViewModel.dataBean.observe(this,dataBean->{
            if(!UIUtils.isEmpty(dataBean)){
                dataBinding.tvTitle.setText(dataBean.getSaying()+"");
                dataBinding.tvTitleDes.setText(dataBean.getTransl()+"");
                dataBinding.tvTitleForm.setText(dataBean.getSource()+"");
            }
        });

        //获取广告
        CSJADTools.loadCSJAdMiddleBanner(getContext(),dataBinding.rlAdBottom,"",CSJ_BANNER_ONE_ID,375,120);
        GDTADTools.loadHFAdByID(getContext(),dataBinding.flAdBottom,GDT_APP_MB_PIC_TWO,375,0 );
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> { finish(); });
    }

    private void initRecycle(){
        newsKnowledgeRecycleAdapter = new NewsKnowledgeRecycleAdapter(R.layout.news_item_knowledge_style);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2);
        dataBinding.recycler.setLayoutManager(layoutManager);
        List<String> listTitle = new ArrayList<>();
        listTitle.add("历史今天");
        listTitle.add("近反义词");
        listTitle.add("简繁转换");
        listTitle.add("新华字典");
        listTitle.add("中文词典");
        listTitle.add("成语典故");
        listTitle.add("英汉词典");
        listTitle.add("诗词鉴赏");
        listTitle.add("名言警句");
        dataBinding.recycler.setAdapter(newsKnowledgeRecycleAdapter);
        newsKnowledgeRecycleAdapter.setNewData(listTitle);
        newsKnowledgeRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                String text = baseQuickAdapter.getData().get(i).toString();
                switch (text){
                    case "历史今天":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_HISTORY_TODAY).navigation();
                        break;
                    case "诗词鉴赏":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_SCJS).navigation();
                        break;
                    case "近反义词":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","近反义词").navigation();
                        break;
                    case "名言警句":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","名言警句").navigation();
                        break;
                    case "成语典故":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","成语典故").navigation();
                        break;
                    case "简繁转换":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","简繁转换").navigation();
                        break;
                    case "新华字典":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","新华字典").navigation();
                        break;
                    case "中文词典":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","中文词典").navigation();
                        break;
                    case "英汉词典":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","英汉词典").navigation();
                        break;
                }
            }
        });
    }

}
